package com.xwm93up.javatips.soap;

import cn.hutool.http.webservice.SoapClient;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Description: 使用SoapClient实现接口调用
 * @Author: xwm93up
 * @Since: 2022/7/19 23:04
 */
public class SoapClientTest {
    public static void main(String[] args) {
        //请求地址
        String soapUrl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
        HashMap<String, Object> map = new HashMap<>();
        map.put("theCityName", "深圳");
        SoapClient soapClient = SoapClient.create(soapUrl)
                //请求方法,命名空间
                .setMethod("web:getWeatherbyCityName", "http://WebXml.com.cn/")
                .setParams(map);
        String sendSoap = soapClient.send(true);
        System.out.println(sendSoap);
        Map<String, String> map1 = new HashMap<String, String>();
        Map soapMap = XmlMap(sendSoap, map1);
        System.out.println(JSONObject.toJSONString(soapMap));

        /*//请求地址
        String soapUrl = "http://ssssss/demo.asmx?wsdl";
        HashMap<String, Object> map = new HashMap<>();
        map.put("startTime", "2022-07-10 00:00:00");
        map.put("endTime", "2022-07-20 00:00:00");
        SoapClient soapClient = SoapClient.create(soapUrl)
                //请求方法,命名空间
                .setMethod("web:GetPresInfo", "http://tempuri.org/")
                .setParams(map);
        String sendSoap = soapClient.send(true);
        System.out.println(sendSoap);
        Map<String, String> map1 = new HashMap<String, String>();
        Map soapMap = XmlMap(sendSoap, map1);
        System.out.println(JSONObject.toJSONString(soapMap));*/
    }

    public static Map<String, String> XmlMap(String xml, Map<String, String> map) {
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(new StringReader(xml));
            Element root = doc.getRootElement();
            String path = "";
            if (map.containsKey(root.getName().trim())) {
                path = map.get(root.getName().trim());
                map.remove(root.getName().trim());
            }
            for (Iterator i = root.elementIterator(); i.hasNext(); ) {
                Element element = (Element) i.next();
                if (element.isTextOnly()) {
                    if (path.length() > 0) {
                        map.put(path + element.getName().trim(), element.getTextTrim());
                    } else {
                        map.put(element.getName().trim(), element.getTextTrim());
                    }
                } else {
                    map.put(element.getName().trim(), path + element.getName().trim() + ".");
                    XmlMap(element.asXML(), map);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
